package com.magina.antiPro;

import org.junit.Test;

import java.math.BigDecimal;

import static org.junit.Assert.assertEquals;

public class Checkchoose {

    public static BigDecimal toFactorial(BigDecimal n){
        if (n.compareTo(BigDecimal.ONE) == 0 ){
            return BigDecimal.ONE;
        }else{
            return n.multiply(toFactorial(n.subtract(BigDecimal.ONE)));
        }
    }
    public static long checkchoose(long m, int x) {
        BigDecimal n = BigDecimal.valueOf((long)x);
        System.out.println(String.format("m=%s,n=%s",m,n));
        // your code
        BigDecimal minNum = BigDecimal.ZERO;
        BigDecimal initNum = BigDecimal.ZERO;
        for(BigDecimal i=BigDecimal.ONE;i.compareTo(n)<0;i=i.add(BigDecimal.ONE)){
            BigDecimal num1 = toFactorial(n);
            BigDecimal num2 = toFactorial(i);
            BigDecimal num3 = toFactorial(n.subtract(i));
            System.out.println(String.format("%s-%s-%s",num1,num2,num3));
            BigDecimal factoralNum =(num2.multiply(num3) );
            factoralNum = num1.divide(factoralNum);
            if(factoralNum.compareTo(BigDecimal.valueOf(m)) ==0  ){
                if(factoralNum.compareTo(minNum)<0 && BigDecimal.ZERO !=minNum){
                    minNum = factoralNum;
                    initNum = i;
                }else if(BigDecimal.ZERO == minNum &&BigDecimal.ZERO==initNum){
                    minNum = factoralNum;
                    initNum = i;
                }
            }
        }
        return initNum == BigDecimal.ZERO ? -1 :initNum.intValue();
    }

    @Test
    public void BasicTests() {
        System.out.println("****** Basic Tests ******");
        System.out.println(toFactorial(BigDecimal.valueOf(21)));
        assertEquals(2, Checkchoose.checkchoose(6, 4));
        assertEquals(1, Checkchoose.checkchoose(4, 4));
        assertEquals(3, Checkchoose.checkchoose(35, 7));
        assertEquals(-1, Checkchoose.checkchoose(4, 2));
        assertEquals(-1, Checkchoose.checkchoose(36, 7));
    }
}
